package com.huanxing.cloud.common.log.event;

import com.huanxing.cloud.common.myabtis.tenant.HxTenantContextHolder;
import com.huanxing.cloud.upms.api.entity.SysLog;
import com.huanxing.cloud.upms.api.entity.SysLoginLog;
import com.huanxing.cloud.upms.api.remote.RemoteSysLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;

/**
 * @author: lijx
 * @date: 2023/6/26 11:51
 **/
@Slf4j
@RequiredArgsConstructor
public class HxLogEventListener {

	private final RemoteSysLogService remoteSysLogService;

	@Async("hxAsyncExecutor")
	@EventListener(HxLogEvent.class)
	public void saveSysLog(HxLogEvent event) {
		SysLog sysLog = event.getSysLog();
		log.info("调用远程接口之前: tenantId = {}", HxTenantContextHolder.getTenantId());

		remoteSysLogService.saveLog(sysLog);
	}

	@Async("hxAsyncExecutor")
	@EventListener(HxLoginLogEvent.class)
	public void saveSysLoginLog(HxLoginLogEvent event) {
		SysLoginLog sysLoginLog = event.getSysLoginLog();
		remoteSysLogService.saveLoginLog(sysLoginLog);
	}

}
